//
//  BanquetHallPickerViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/10/21.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit
import PickerView

class BanquetHallPickerViewController: UIViewController {

    var banquetHalls: [RestaurantBanquetHall] = []
    
    var currentSelectedValue: RestaurantBanquetHall?
    
    var updateSelectedValue: ((_ newSelectedValue: RestaurantBanquetHall) -> Void)?
    
    @IBOutlet weak var pickerView: PickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configurePickerView()
        // Do any additional setup after loading the view.
    }

    fileprivate func configurePickerView() {
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.scrollingStyle = .default
        pickerView.selectionStyle = .defaultIndicator
        if let currentSelected = currentSelectedValue, let index = banquetHalls.index(where: { bh in
            if bh.id == currentSelected.id {
                return true
            } else {
                return false
            }
        }){
            pickerView.currentSelectedRow = index
        }

    }
    
    @IBAction func cancel(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func done(_ sender: UIBarButtonItem) {
        if let updateValue = updateSelectedValue, let currentSelected = currentSelectedValue {
            updateValue(currentSelected)
        }
        dismiss(animated: true, completion: nil)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension BanquetHallPickerViewController: PickerViewDataSource {
    
    // MARK: - PickerViewDataSource
    
    func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int {
        return banquetHalls.count
    }
    
    func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return banquetHalls[index].name ?? ""
    }
    
}

extension BanquetHallPickerViewController: PickerViewDelegate {
    
    // MARK: - PickerViewDelegate
    
    func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat {
        return 50.0
    }
    
    func pickerView(_ pickerView: PickerView, didSelectRow row: Int, index: Int) {
        currentSelectedValue = banquetHalls[index]
    }
    
    func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool) {
        label.textAlignment = .center
        if #available(iOS 8.2, *) {
            if (highlighted) {
                label.font = UIFont.systemFont(ofSize: 26.0, weight: UIFontWeightLight)
            } else {
                label.font = UIFont.systemFont(ofSize: 16.0, weight: UIFontWeightLight)
            }
        } else {
            if (highlighted) {
                label.font = UIFont(name: "HelveticaNeue-Light", size: 16.0)
            } else {
                label.font = UIFont(name: "HelveticaNeue-Light", size: 26.0)
            }
        }
        
        if (highlighted) {
            label.textColor = view.tintColor
        } else {
            label.textColor = UIColor(red: 161.0/255.0, green: 161.0/255.0, blue: 161.0/255.0, alpha: 1.0)
        }
    }
    
}
